/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package udio;
/**
 *
 * @author pat
 */
import java.sql.*;
public class Vehicle {

    private DB db;

    public Vehicle() {
        this.db = new DB();
    }
    public void add(int vehicleClass, int vehicleID, int depotID, String make, String model, String color, int year){
        Connection conn = db.connect();
        String query = "INSERT INTO VehicleDB (vehicleclass,depotId,make,model,color,year) "
                +"VALUES ('" + vehicleClass + "','" + depotID + "','" + make + "','" + model + "','" + color + "','" + year
                + "')";
        db.putQuery(conn, query);
        db.disconnect(conn);
    }

    public void removeVehicle(int vehicleID){
        Connection conn = db.connect();
        String query = "DELETE FROM VehicleDB WHERE vehicleId = " + vehicleID;
        db.putQuery(conn, query);
        db.disconnect(conn);
    }

    public int getDepotId(int vehicleID){
        int depotID = -1;
        Connection conn = db.connect();
        String query = "SELECT depotId FROM VehicleDB WHERE vehicleId = " + vehicleID;
        ResultSet result = db.getQuery(conn,query);
        db.disconnect(conn);
        try{
            result.next();
            depotID = result.getInt("depotId");
        }catch(Exception e) {
            System.out.println( e.getMessage());
        }
        return depotID;
    }

    public int getVehicleClass(int vehicleID){
        int vehicleClass = -1;
        Connection conn = db.connect();
        String query = "SELECT vehicleClass FROM VehicleDB WHERE vehicleId = " + vehicleID;
        ResultSet result = db.getQuery(conn,query);
        db.disconnect(conn);
        try{
            result.next();
            vehicleClass = result.getInt("vehicleClass");
        }catch(Exception e){ }
        return vehicleClass;
    }

    public void setDepot(int vehicleID, int depotID){
        Connection conn = db.connect();
        String query = "UPDATE VehicleDB SET depotId = " + depotID
                + " WHERE vehicleId = " + vehicleID;
        db.putQuery(conn, query);
        db.disconnect(conn);
    }
}
